<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="width: 100%;"
  >
    <el-col>
      <strong v-if="isRoot">{{ treeData.name }}</strong>
      <span v-else>{{ treeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown>
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDepts">添加子部门</el-dropdown-item>
              <!-- 编辑部门和删除部门只会在子节点上显示 -->
              <el-dropdown-item v-if="!isRoot" @click.native="editDepts">编辑此部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepts">删除此部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { deleteDepartments } from '@/api/departments'
export default {
  props: {
    treeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    addDepts() {
      console.log('新增')
      this.$emit('addDepts', this.treeData)
    },
    editDepts() {
      console.log('编辑')
    },
    async delDepts() {
      try {
        console.log('删除')
        // 先确认删除
        await this.$confirm('是否确认删除该部门?')
        // 发送请求
        await deleteDepartments(this.treeData.id)
        // 提示成功
        this.$message.success('删除成功')
        // 通知父组件更新
        this.$emit('delDepts')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
